package com.yinhechuangji.application.configer;

import com.alibaba.druid.pool.DruidDataSource;
import com.yinhechuangji.application.entity.Test;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Map;

/**
* @Auther: yaodx
* @Date: 2018/9/6 15:10
* @Description:配置Datasource
*/
@Configuration
//@RefreshScope                                                         //此注解不能和@Configuration同时用
public class DruidConfiguration {

//    @Value("${spring.datasource.poolPreparedStatements}")
//    private boolean poolPreparedStatements;
//    @Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize}")
//    private int maxPoolPreparedStatementPerConnectionSize;
//    @Value("${spring.datasource.filters}")
//    private String filters;
//    @Value("${spring.datasource.connectionProperties}")
//    private String connectionProperties;
//    @Value("${spring.datasource.useGlobalDataSourceStat}")
//    private boolean useGlobalDataSourceStat;

    /*
     * @Auther: yaodx
     * @Date: 2018/9/4 15:00
     * @Description:数据源配置
     */
    @Bean()     //声明其为Bean实例
    @RefreshScope
    @Primary()  //在同样的DataSource中，首先使用被标注的DataSource
    public DataSource dataSource(){
        DruidDataSource datasource = new DruidDataSource();
        Test test=SpringUtil.getBean(Test.class);                                                   //从容器中取出为dataSource刷新数据的Bean
        Map map=test.getInstance();
        datasource.setUrl((String)map.get("dbUrl"));
        datasource.setUsername((String)map.get("username"));
        datasource.setPassword((String)map.get("password"));
        datasource.setDriverClassName((String)map.get("driverClassName"));

        //configuration
        datasource.setInitialSize((Integer) map.get("initialSize"));
        datasource.setMinIdle((Integer)map.get("minIdle"));
        datasource.setMaxActive((Integer)map.get("maxActive"));
//        datasource.setMaxWait(maxWait);
        datasource.setTimeBetweenEvictionRunsMillis((Integer)map.get("timeBetweenEvictionRunsMillis"));
        datasource.setMinEvictableIdleTimeMillis((Integer)map.get("minEvictableIdleTimeMillis"));
        datasource.setValidationQuery((String)map.get("validationQuery"));
        datasource.setTestWhileIdle((boolean)map.get("testWhileIdle"));
        datasource.setTestOnBorrow((boolean)map.get("testOnBorrow"));
        datasource.setTestOnReturn((boolean)map.get("testOnReturn"));

//        datasource.setPoolPreparedStatements(poolPreparedStatements);
//        datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
//        datasource.setUseGlobalDataSourceStat(useGlobalDataSourceStat);
        try {
//            datasource.setFilters(filters);
            datasource.setFilters("stat,wall");
        } catch (SQLException e) {
//            log.error("druid configuration initialization filter: "+ e);
        }
//        datasource.setConnectionProperties(connectionProperties);
        datasource.setConnectionProperties("druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000");
        return datasource;
    }

}


